//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.cqw.utils.data;

import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;


public class AESEncoder {
    private final SecretKey secretKey;
    private final String ECB_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    public AESEncoder(String accessKey) {
        this.secretKey = new SecretKeySpec(Base64.getDecoder().decode(accessKey), "AES");
    }

    public String encode(String plainText) {
        if (StringUtils.isBlank(plainText)) {
            return "";
        } else {
            try {
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(1, this.secretKey);
                byte[] bytes = plainText.getBytes("UTF-8");
                return Base64.getEncoder().encodeToString(cipher.doFinal(bytes));
            } catch (Exception var4) {
                throw new RuntimeException("AES encode error.", var4);
            }
        }
    }

    public String decode(String encodedText) {
        if (StringUtils.isBlank(encodedText)) {
            return "";
        } else {
            try {
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(2, this.secretKey);
                byte[] bytes = Base64.getDecoder().decode(encodedText);
                return new String(cipher.doFinal(bytes), "UTF-8");
            } catch (Exception var4) {
                throw new RuntimeException("AES decode error.", var4);
            }
        }
    }
}
